package com.purchase.jd.start;

import com.purchase.jd.commodity.JdConstants;
import com.purchase.jd.job.JdRushToPurchase;
import com.purchase.jd.login.Login;
import com.purchase.util.JudgeUtils;
import com.purchase.util.ScheduledExecutor;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author saw
 * @date 2021年12月22 10:32
 * @description 京东抢购方法
 */
public class Purchase {

    /**
     * 京东抢购     抢购前先预约，暂未提供预约功能
     * @param args
     * @throws IOException
     * @throws InterruptedException
     * @throws ExecutionException
     */
    public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
        //登录
        Login.jdLogin();
        //重写静态变量，商品id  茅台：100012043978
        JdConstants.SKU_ID = "52593127754";
        //浏览器F12控制台 _JdEid    获取
        JdConstants.EID = "NR3L4N3AHB7GRYLZVI5UXBFBNM23XQQVC6BOEDN33YN3UO77FRPJEI5Q73COOQKYUBTCDAAYZWY6IEVRDK26G7IWZU";
        //浏览器F12控制台 _JdJrTdRiskFpInfo    获取
        JdConstants.FP = "322073933cb3fbd319de50a128e0f906";
        //抢购数量
        JdConstants.NEED_COUNT = 1;
        //TODO:默认处理器核心线程数 如有需要请自己修改
        JdConstants.THREADS = 1;
        //TODO:如果已经加入购物车，请填写购物车内对应的数量
//        JdConstants.SHOPPING_COUNT = new AtomicInteger(0);
        //获取启动时间
        long startTime = JudgeUtils.getJdPurchaseTime();
        for(int i= 0; i < JdConstants.THREADS; i++){
            //延迟启动
            Future<Integer> future = ScheduledExecutor.EXECUTOR
                    .schedule(new JdRushToPurchase(),startTime, TimeUnit.MILLISECONDS);
            int num = future.get();
            if(num >= JdConstants.NEED_COUNT){
                ScheduledExecutor.EXECUTOR.shutdown();
            }
        }
    }
}
